<template>
  <div id="dictionaryInfo" class="app-container calendar-list-container">
    <el-row style="margin-bottom:5px" :gutter="20">
      <el-col :span="6">
        <el-button :size="size" type="primary" icon="el-icon-search" @click="getList">搜索</el-button>
        <el-button :size="size" type="primary" icon="el-icon-search" @click="handleCreate">新增</el-button>
      </el-col>
    </el-row>
    <el-card class="box-card">
      <el-table
        :data="tableData"
        border
        fit
        highlight-current-row
        stripe
        :size="size"
        style="width: 100%"
        element-loading-text="给我一点时间"
        :header-cell-class-name="tableRowClassName"
        lazy
        :load="load"
        row-key="ID"
        ref="table"
      >
        <el-table-column label="节点ID" prop="ID" />
        <el-table-column label="节点名称" prop="Name" />
        <el-table-column label="排序" prop="SortNO" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-dropdown>
              <el-button type="primary">
                更多菜单<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown" divided>
                <el-dropdown-item>
                  <el-button size="mini" type="primary" @click="handelUpdate(scope.row)">修改</el-button>
                </el-dropdown-item>
                <el-dropdown-item divided>
                  <el-button size="mini" type="primary" @click="handelDetail(scope.row)">详情</el-button>
                </el-dropdown-item>
                <el-dropdown-item divided>
                  <el-button size="mini" type="danger" @click="handelDelete(scope.row)">删除</el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: center; margin-top: 15px">
        <el-pagination :current-page="1" :page-sizes="[10, 20, 30, 50]" :page-size="1" layout="total, sizes, prev, pager, next, jumper" :total="total" style="text-align: center" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>
    <el-dialog :visible.sync="dialogState" :title="title" :modal-append-to-body="false" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="节点名称" prop="Name">
              <el-input v-model="form.Name" :size="size" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父级节点">
              <el-cascader v-model="form.ParentID" :size="size" :options="parentSelectOption" placeholder="请选择父节点" :props="parentSelectProp" :show-all-levels="false" style="width:100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序">
              <el-input v-model.number="form.SortNO" :size="size" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="text-align:center">
          <el-button v-if="!dialogDisable" type="primary" @click="submit">提交</el-button>
          <el-button type="danger" @click="dialogState=false">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import {
  getLazyNode,
  createNode,
  updateNode,
  deleteNode,
  getAllDictionaryNode
} from '@/api/dictionary'
export default {
  name: 'DictionaryInfo',
  components: {},
  filters: {},
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10
      },
      tableData: [],
      dialogState: false,
      dialogDisable: false,
      form: {
        Name: null,
        ParentID: null,
        SortNO: null
      },
      rules: {
        Name: [{ required: true, message: '请输入节点名称', trigger: 'blur' }]
      },
      total: 0,
      title: '新增',
      parentSelectOption: [],
      parentSelectProp: {
        value: 'ID',
        label: 'Name',
        checkStrictly: true,
        multiple: false,
        emitPath: false
      },
      maps: new Map()
    }
  },
  computed: {
    ...mapGetters(['size']),
    getRoleLevel() {
      if (this.store.state.user.roleLevel === 'admin') {
        return true
      } else {
        return true
      }
    },
    headers() {
      return {
        'X-Token': getToken()
      }
    }
  },
  watch: {
    dialogState(val) {
      if (val) {
        this.getOptions()
      }
    }
  },
  created() {},
  mounted() {
    this.getList()
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 0) {
        return 'el-button--primary is-active'
      }
      return ''
    },
    getList() {
      getLazyNode(this.listQuery).then((res) => {
        this.tableData = res.data.model.Items
        this.total = res.data.model.Total
      })
    },
    getOptions() {
      getAllDictionaryNode().then((res) => {
        this.parentSelectOption = res.data.model
      })
    },
    load(tree, treeNode, resolve) {
      this.maps.set(tree.ID, { tree, treeNode, resolve })
      let temp = {
        ParentId: tree.ID
      }
      getLazyNode(temp).then((res) => {
        if (res.data.model != null) {
          resolve(res.data.model.Items)
          console.log(res.data.model.Items)
        } else {
          resolve([])
        }
      })
    },
    resetForm() {
      this.form = {
        Name: null,
        ParentID: null,
        SortNO: null
      }
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.title === '新增') {
            createNode(this.form).then((res) => {
              this.dialogState = false
              this.reRenderChildrenNode(this.form.ParentID)
            })
          } else {
            updateNode(this.form).then((res) => {
              this.dialogState = false
              this.reRenderChildrenNode(this.form.ParentID)
            })
          }
        }
      })
    },
    handleCreate() {
      this.resetForm()
      this.dialogDisable = false
      this.dialogState = true
      this.title = '新增'
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    handelUpdate(row) {
      this.resetForm()
      this.dialogState = true
      this.dialogDisable = false
      this.title = '修改'
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.form = Object.assign({}, row)
        this.form.CreateBy = this.$store.state.user.ID
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handelDetail(row) {
      this.title = '查看详情'
      this.dialogState = true
      this.dialogDisable = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.form = Object.assign({}, row)
      })
    },
    handelDelete(row) {
      this.$confirm('确定要删除吗,此操作不可恢复?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let temp = {
          ID: row.ID
        }
        deleteNode(temp).then(res => {
          this.reRenderChildrenNode(row.ParentID)
        })
      })
    },
    close() {
      this.dialogState = false
      this.dialogDisable = false
    },
    reRenderChildrenNode(ID) {
      if (this.maps.has(ID)) {
        const { tree, treeNode, resolve } = this.maps.get(ID)
        this.$set(this.$refs.table.store.states.lazyTreeNodeMap, ID, [])
        this.load(tree, treeNode, resolve)
      } else {
        this.getList()
      }
    }
  }
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
</style>
